package com.example.recruitment.controller;

import com.example.recruitment.entity.Job;
import com.example.recruitment.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/job")
public class JobController {

    @Autowired
    private JobService jobService;

    @GetMapping("/list")
    public String list(@RequestParam(required = false) String title,
                      @RequestParam(required = false) String department,
                      @RequestParam(required = false) String status,
                      Model model) {
        // 创建查询条件对象
        Job query = new Job();
        query.setTitle(title);
        query.setDepartment(department);
        query.setStatus(status);
        
        // 查询职位列表
        List<Job> jobs = jobService.findAll(query);
        model.addAttribute("jobs", jobs);
        
        // 添加部门列表供选择
        List<String> departments = Arrays.asList("技术部", "产品部", "设计部", "运营部", "市场部", "销售部");
        model.addAttribute("departments", departments);
        
        // 保存搜索条件
        model.addAttribute("title", title);
        model.addAttribute("department", department);
        model.addAttribute("status", status);
        
        return "job/list";
    }

    @GetMapping("/{id}")
    @ResponseBody
    public Job getJob(@PathVariable Long id) {
        return jobService.findById(id);
    }

    @GetMapping("/view/{id}")
    public String view(@PathVariable Long id, Model model) {
        Job job = jobService.findById(id);
        if (job == null) {
            return "redirect:/job/list";
        }
        model.addAttribute("job", job);
        return "job/view";
    }

    @GetMapping("/add")
    public String addForm(Model model) {
        // 添加部门列表供选择
        List<String> departments = Arrays.asList("技术部", "产品部", "设计部", "运营部", "市场部", "销售部");
        model.addAttribute("departments", departments);
        model.addAttribute("job", new Job());
        return "job/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public String add(@RequestBody Job job) {
        if (jobService.add(job)) {
            return "success";
        }
        return "fail";
    }

    @GetMapping("/edit/{id}")
    public String editForm(@PathVariable Long id, Model model) {
        Job job = jobService.findById(id);
        if (job == null) {
            return "redirect:/job/list";
        }
        // 添加部门列表供选择
        List<String> departments = Arrays.asList("技术部", "产品部", "设计部", "运营部", "市场部", "销售部");
        model.addAttribute("departments", departments);
        model.addAttribute("job", job);
        return "job/edit";
    }

    @PostMapping("/edit/{id}")
    @ResponseBody
    public String edit(@PathVariable Long id, @RequestBody Job job) {
        job.setId(id);
        // 确保状态值有效
        if (job.getStatus() == null) {
            job.setStatus("1"); // 默认为招聘中
        }
        if (jobService.update(job)) {
            return "success";
        }
        return "fail";
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public String delete(@PathVariable Long id) {
        if (jobService.deleteById(id)) {
            return "success";
        }
        return "fail";
    }

    @PostMapping("/status/{id}")
    @ResponseBody
    public String updateStatus(@PathVariable Long id, @RequestParam String status) {
        if (jobService.updateStatus(id, status)) {
            return "success";
        }
        return "fail";
    }

    @GetMapping("/department/{department}")
    public String findByDepartment(@PathVariable String department, Model model) {
        List<Job> jobs = jobService.findByDepartment(department);
        model.addAttribute("jobs", jobs);
        return "job/list";
    }
} 